package Structure;

public class Practice {
    public static void main(String[] args) {
        int[] arr = {13, 26, -3, 4, 54, 26, 37, 18, 69, -10};
        sort(arr, false);
        for (int i = 0; i < 10; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        myPrint(arr);

        System.out.println(multiply(6, -3));

        long minute = 3456789;
        convert(minute);

        method();

        int[] a = {1, 2, 1, 3, 3, 3, 6, 1, 5, 3, 4, 6};
        for (int value : filtArray(a)) {
            System.out.print(value + " ");
        }
    }

    static void sort(int[] arr, boolean isAsc) {

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i; j < arr.length; j++) {
                if ((isAsc && arr[i] > arr[j]) || (!isAsc && arr[i] < arr[j])) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    // 冒泡排序
    static void sort(int[] arr) {
        for (int i = arr.length - 1; i > 0; i--) {
            boolean sorted = true;
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    sorted = false;
                }
            }
            if (sorted) {
                break;
            }
        }
    }


    //    定义一个数组，内容 {13,26,-3,4,54,26,37,18,69,-10}，
//    编写一个print方法，使用字符串拼接的方式，将数组内容数组输出为如下格式，每5个换行
    static void myPrint(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print("[" + i + "]=" + arr[i] + "  ");
            if ((i + 1) % 5 == 0) {
                System.out.println();
            }
        }
    }


    //    编写一个方法不使用 * 完成两个数相乘
    static int multiply(int n1, int n2) {
        int result = 0;
        int temp = n2;
        temp = Math.abs(temp);
        for (int i = 0; i < temp; i++) {
            result += n1;
        }
        if (n2 < 0)
            result = -result;
        return result;
    }


    //    编写一个方法输入分钟时间，向控制台输出多少年，多少天
    static void convert(long minute) {
        long year = 0, day = 0;
        year = minute / 60 / 24 / 365;
        day = minute / 60 / 24 - year * 365;
        System.out.println(minute + " 分钟是" + year + "年" + day + "天");
    }


    //    有1.5元钱兑换1分，2分，5分硬币100枚，每种面值至少一个，请输出所有的兑换方案，并统计方案的总数
    static void method() {
        int count = 0;
        for (int i = 1; i < 75; i++) {
            for (int j = 1; j < 30; j++) {
                if (2 * i + 5 * j + (100 - i - j) == 150) {
                    count++;
                    System.out.println((100 - i - j) + "个1分," + i + "个2分," + j + "个5分");
                }
            }
        }
        System.out.println("一共有" + count + "种可能");
    }

    //    编写一个方法用于int数组去重，将数组中重复的元素只保留一个
    static int[] filtArray(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    arr[j] = arr[length - 1];
                    j--;
                }
            }
        }
        int[] result = new int[length];
        for (int i = 0; i < length; i++) {
            result[i] = arr[i];
        }
        return result;
    }
}

